package com.maket.jbxx.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.maket.jbxx.domain.vo.TreeSelect;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.maket.jbxx.mapper.GcFactoryUnitTableMapper;
import com.maket.jbxx.domain.GcFactoryUnitTable;
import com.maket.jbxx.service.IGcFactoryUnitTableService;

import javax.annotation.Resource;

/**
 * 工厂定义Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-25
 */
@Service
public class GcFactoryUnitTableServiceImpl implements IGcFactoryUnitTableService 
{
    @Resource
    private GcFactoryUnitTableMapper gcFactoryUnitTableMapper;

    /**
     * 查询工厂定义
     * 
     * @param dyCode 工厂定义主键
     * @return 工厂定义
     */
    @Override
    public GcFactoryUnitTable selectGcFactoryUnitTableByDyCode(String dyCode)
    {
        return gcFactoryUnitTableMapper.selectGcFactoryUnitTableByDyCode(dyCode);
    }

    /**
     * 查询工厂定义列表
     * 
     * @param gcFactoryUnitTable 工厂定义
     * @return 工厂定义
     */
    @Override
    public List<GcFactoryUnitTable> selectGcFactoryUnitTableList(GcFactoryUnitTable gcFactoryUnitTable)
    {
        return gcFactoryUnitTableMapper.selectGcFactoryUnitTableList(gcFactoryUnitTable);
    }

    @Override
    public List<GcFactoryUnitTable> selectGcFactoryDyCode(GcFactoryUnitTable gcFactoryUnitTable) {
        return gcFactoryUnitTableMapper.selectGcFactoryDyCode(gcFactoryUnitTable);
    }

    @Override
    public List<GcFactoryUnitTable> selectGcFactoryUnitTableByList(GcFactoryUnitTable gcFactoryUnitTable) {
        return gcFactoryUnitTableMapper.selectGcFactoryUnitTableByList(gcFactoryUnitTable);
    }


    /**
     * 查询部门树结构信息
     *
     * @param dept 部门信息
     * @return 部门树信息集合
     */
    @Override
    public List<TreeSelect> selectDeptTreeList(GcFactoryUnitTable dept)
    {
        List<GcFactoryUnitTable> depts = selectGcFactoryUnitTableList(dept);
        return buildDeptTreeSelect(depts);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<GcFactoryUnitTable> buildDeptTree(List<GcFactoryUnitTable> depts)
    {
        List<GcFactoryUnitTable> returnList = new ArrayList<GcFactoryUnitTable>();

        List<String> tempList = depts.stream().map(GcFactoryUnitTable::getDyCode).collect(Collectors.toList());
        for (GcFactoryUnitTable dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getDyParentCode()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param depts 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<GcFactoryUnitTable> depts)
    {
        List<GcFactoryUnitTable> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<GcFactoryUnitTable> list, GcFactoryUnitTable t)
    {
        // 得到子节点列表
        List<GcFactoryUnitTable> childList = getChildList(list, t);
        t.setChildren(childList);
        for (GcFactoryUnitTable tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<GcFactoryUnitTable> getChildList(List<GcFactoryUnitTable> list, GcFactoryUnitTable t)
    {

        List<GcFactoryUnitTable> tlist = new ArrayList<GcFactoryUnitTable>();
        Iterator<GcFactoryUnitTable> it = list.iterator();
        while (it.hasNext())
        {
            GcFactoryUnitTable n = it.next();
            if (StringUtils.isNotNull(n.getDyParentCode()) && Objects.equals(n.getDyParentCode(), t.getDyCode()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<GcFactoryUnitTable> list, GcFactoryUnitTable t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 新增工厂定义
     * 
     * @param gcFactoryUnitTable 工厂定义
     * @return 结果
     */
    @Override
    public int insertGcFactoryUnitTable(GcFactoryUnitTable gcFactoryUnitTable)
    {
        return gcFactoryUnitTableMapper.insertGcFactoryUnitTable(gcFactoryUnitTable);
    }

    /**
     * 修改工厂定义
     * 
     * @param gcFactoryUnitTable 工厂定义
     * @return 结果
     */
    @Override
    public int updateGcFactoryUnitTable(GcFactoryUnitTable gcFactoryUnitTable)
    {
        return gcFactoryUnitTableMapper.updateGcFactoryUnitTable(gcFactoryUnitTable);
    }

    /**
     *修改工厂定义的启禁用
     */
    @Override
    public int updateGcFactoryUnitTableIsStart(GcFactoryUnitTable gcFactoryUnitTable) {
        return gcFactoryUnitTableMapper.updateGcFactoryUnitTableIsStart(gcFactoryUnitTable);
    }


    /**
     * 批量删除工厂定义
     * 
     * @param dyCodes 需要删除的工厂定义主键
     * @return 结果
     */
    @Override
    public int deleteGcFactoryUnitTableByDyCodes(String[] dyCodes)
    {
        return gcFactoryUnitTableMapper.deleteGcFactoryUnitTableByDyCodes(dyCodes);
    }

    /**
     * 删除工厂定义信息
     * 
     * @param dyCode 工厂定义主键
     * @return 结果
     */
    @Override
    public int deleteGcFactoryUnitTableByDyCode(String dyCode)
    {
        return gcFactoryUnitTableMapper.deleteGcFactoryUnitTableByDyCode(dyCode);
    }
}
